package cn.flant.db.hdle.dialect;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import cn.flant.db.hdle.ExecuteParam;
import cn.flant.db.hdle.key.PrimaryKey;

public class AnsiDialect extends Dialect {

	@Override
	public ExecuteParam buildSelect(String selectFields, String tableName, String[] fields, Object[] params) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT ").append(selectFields).append(" FROM ").append(tableName).append(" WHERE ");
		boolean flag = false;
		for (String pk : fields) {
			if (flag) {
				sb.append(" AND ");
			} else {
				flag = true;
			}
			sb.append(pk).append("=?");
		}
		ExecuteParam param = new ExecuteParam();
		param.setSql(sb.toString());
		param.setParams(params);
		return param;
	}

	@Override
	public ExecuteParam buildSave(String tableName, PrimaryKey pkInfo, Map<String, Object> values) {
		List<Object> list = new ArrayList<Object>();
		StringBuilder fsb = new StringBuilder();
		StringBuilder psb = new StringBuilder();
		for (Entry<String, Object> entry : values.entrySet()) {
			Object value = entry.getValue();
			if (value == null) {
				continue;
			}
			if (fsb.length() > 0) {
				fsb.append(",");
				psb.append(",");
			}
			fsb.append(entry.getKey());
			psb.append("?");
			list.add(value);
		}
		StringBuilder sb = new StringBuilder();
		sb.append("INSERT INTO ").append(tableName).append(" (").append(fsb).append(") VALUES (").append(psb).append(")");
		ExecuteParam param = new ExecuteParam();
		param.setSql(sb.toString());
		param.setParams(list.toArray(new Object[list.size()]));
		return param;
	}

	@Override
	public ExecuteParam buildUpdate(String tableName, Map<String, Object> setParams, Map<String, Object> keyParams) {
		boolean flag = false;
		List<Object> list = new ArrayList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append("UPDATE ").append(tableName).append(" SET ");
		for (Entry<String, Object> entry : setParams.entrySet()) {
			String field = entry.getKey();
			Object value = entry.getValue();
			if (flag) {
				sb.append(",");
			} else {
				flag = true;
			}
			sb.append(field).append("=?");
			list.add(value);
		}
		sb.append(" WHERE ");
		flag = false;
		for (Entry<String, Object> entry : keyParams.entrySet()) {
			String field = entry.getKey();
			Object value = entry.getValue();
			if (flag) {
				sb.append(" AND ");
			} else {
				flag = true;
			}
			sb.append(field).append("=?");
			list.add(value);
		}
		Object[] pa = new Object[list.size()];
		list.toArray(pa);
		ExecuteParam param = new ExecuteParam();
		param.setSql(sb.toString());
		param.setParams(pa);
		return param;
	}

	@Override
	public ExecuteParam buildDelete(String tableName, String[] fields, Object[] params) {
		StringBuilder sb = new StringBuilder();
		sb.append("DELETE FROM ").append(tableName).append(" WHERE ");
		boolean flag = false;
		List<Object> list = new ArrayList<Object>();
		for (int i = 0; i < fields.length; i++) {
			String field = fields[i];
			Object value = params[i];
			if (flag) {
				sb.append(" AND ");
			} else {
				flag = true;
			}
			sb.append(field).append("=?");
			list.add(value);
		}
		ExecuteParam param = new ExecuteParam();
		param.setSql(sb.toString());
		param.setParams(list.toArray(new Object[list.size()]));
		return param;
	}

	@Override
	public String buildPageSql(String selectSql, int pageSize, int pageNo) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean isSupportAutoGenerateKey() {
		return false;
	}

}
